package cn.kgc.vue.controller;

import cn.hutool.core.util.RandomUtil;
import cn.kgc.vue.annotatios.LogOperation;
import cn.kgc.vue.commons.Const;
import cn.kgc.vue.commons.QueryCondition;
import cn.kgc.vue.commons.ResponseResult;
import cn.kgc.vue.entity.User;
import cn.kgc.vue.enums.OperationType;
import cn.kgc.vue.form.UserForm;
import cn.kgc.vue.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * @author 课工场
 * @date 2024/6/28
 * @description 后端如何处理跨域问题
 * 1.  在允许跨域的方法上或类上添加注解 @CrossOrigin
 * 添加在方法上 只对当前方法有效
 * 添加在类上 则类中所有的方法均允许跨域
 * 2.全局跨域配置
 * 创建配置类实现WebMvcConfigurer接口 重写 addCorsMappings
 */
@RestController
@RequestMapping("user")
// @CrossOrigin
@Api(tags = "用户模块")
public class UserController {

    @Resource
    private UserService userService;

    @GetMapping("list")
    public List<User> selectUsers() {
        List<User> list = userService.list();
        return list;
    }


    // @CrossOrigin  //  该接口允许跨域请求
    @ApiOperation("验证码接口")
    @GetMapping("captcha")
    public ResponseResult getCaptcha() {
        return userService.getCaptcha();
    }

    /*
        用户登录
        流程分析：
            1.  接收参数
            2.  验证验证码
                1. uuid    redis   code
                   code  null    验证码失效
                   code == formCode   true   false
            3.  shiro    subject  login    filter  session   cookie    token   jwt

    */

    @ApiOperation("用户认证")
    @PostMapping("login")
    public ResponseResult login(@RequestBody HashMap<String, Object> params, HttpServletRequest request) {
        System.out.println("params = " + params);
        return userService.login(params, request);
    }

    @RequiresPermissions("sys:user:select")
    @ApiOperation("用户分页接口")
    @PostMapping("page")
    public ResponseResult userPage(@RequestBody QueryCondition<User> queryCondition) {

        return  userService.userPage(queryCondition);
    }


    @RequiresPermissions("sys:user:state")
    @ApiOperation("用户状态修改")
    @PutMapping("state")
    public ResponseResult userState(@RequestBody HashMap<String,Object> params){

        Integer state = (Integer) params.get("state");
        Integer id = (Integer) params.get("id");

        User user = new User();
        user.setState(state);
        user.setId(id);

        userService.updateById(user);

        return ResponseResult.success().message("状态修改成功");
    }


    @RequiresPermissions("sys:user:delete")
    @ApiOperation("删除用户")
    @DeleteMapping("delete")
    public ResponseResult delete(@RequestBody List<Integer> ids){
        return userService.deleteUser(ids);
    }


    @ApiOperation("重置密码")
    @PutMapping("password/{id}")
    public ResponseResult editPassword(@PathVariable("id") Integer id){

        // 生成随机盐
        String salt = RandomUtil.randomString(8);
        String md5Password = new Md5Hash("111111", salt, 10).toHex();

        User user = new User();
        user.setSalt(salt);
        user.setId(id);
        user.setPassword(md5Password);
        userService.updateById(user);

        return  ResponseResult.success().message("密码重置成功");
    }



    @LogOperation(operation = OperationType.ADD,model = "用户模块")
    @RequiresPermissions("sys:user:add")
    @ApiOperation("添加用户")
    @PostMapping ("add")
    public ResponseResult addUser(@RequestBody UserForm userForm){

        return  userService.addUser(userForm);
    }


    @LogOperation(operation = OperationType.UPDATE,model = "用户模块")
    @RequiresPermissions("sys:user:update")
    @ApiOperation("编辑用户")
    @PutMapping("edit")
    public ResponseResult editUser(@RequestBody UserForm userForm){

        return  userService.editUser(userForm);
    }


    @ApiOperation("获取个人资料")
    @GetMapping("personal")
    public ResponseResult getPersonalInfo(HttpServletRequest request){

        return userService.getPersonalInfo(request) ;
    }

    @ApiOperation("编辑个人资料")
    @PutMapping("editPersonalInfo")
    public ResponseResult editPersonalInfo(@RequestBody User user){

        userService.updateById(user);

        return  ResponseResult.success().message("个人信息编辑成功") ;
    }


    // 编辑个人密码  editPersonalPassword
    @ApiOperation("编辑个人密码")
    @PutMapping("editPersonalPassword")
    public ResponseResult editPersonalPassword(@RequestBody HashMap<String,String> params,HttpServletRequest request){

        return   userService.editPersonalPassword(request,params);
    }


    // 退出登录
    @ApiOperation("安全退出")
    @GetMapping ("logout")
    public ResponseResult logout(HttpServletRequest request){

        return   userService.logout(request);
    }


    // 获取用户权限菜单
    @ApiOperation("权限菜单")
    @GetMapping ("menu")
    public ResponseResult userMenu(HttpServletRequest request){

        return  userService.userMenu(request);
    }


    // 未认证 重定向的接口
    @GetMapping("unAuth")
    public ResponseResult unAuthentication() {
        ResponseResult responseResult = ResponseResult.fail().code(Const.UN_AUTH).message("请认证后访问");
        return responseResult;
    }
}
